<?php

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
/**
 * $Rev: 40 $
 * $Id: core.php 40 2011-08-25 15:09:31Z superbounou $
 * $Author: superbounou $
 * $Date: 2011-08-25 15:09:31 +0000 (Thu, 25 Aug 2011) $
 * 
 * Core function
 */

/* include the framework */
include ('lyna/core/class.app.php');
include ('lyna/core/class.mysqldata.php');
include ('lyna/core/class.action.php');
include ('lyna/core/class.helper.php');

/**
 * Gestion des assertions
 */
function assertHandler($file, $line, $code) {
	$extra = '';
	if (preg_match('/sql/i', $code)) {
		$extra = mysql_error();
	}
	echo '<hr>Assertion failed :<br /><b>File</b> : ' . $file . '<br /><b>Line</b> : ' . $line . '<br /><b>Code</b> : ' . $code . '<br />';
	if (strlen($extra)) {
		echo '<b>Extra : </b>' . $extra;
	}
	echo '<hr />';
	die();
}

assert_options(ASSERT_CALLBACK, 'assertHandler');
/*
function errorHandler($errno, $errstr, $errfile, $errline) {

}

set_error_handler('errorHandler');
*/
/**
* 
*/
function __autoload($className) {
	assert('preg_match(\'/[a-zA-Z]+/i\', $className)');
	$directories = array (
		'action',
		'core',
		'helper'
	);

	$classname = strtolower($className);
	$classfile = 'data' . DIRECTORY_SEPARATOR . 'class.' . $classname . '.php';
	if (file_exists($classfile)) {
		include ($classfile);
	} else {
		foreach ($directories as $directory) {
			$pos = strpos($classname, $directory);
			if ($pos !== false) {
				$classfile = substr($classname, 0, $pos);
				$classfile = $directory . DIRECTORY_SEPARATOR . $directory . '.' . $classfile . '.php';
				assert('/*Include class file ' . $classfile . '*/file_exists($classfile)');
				include ($classfile);
			}
		}
	}
}
?>
